﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginarium.team;

namespace Imaginarium.IA
{
    public class TeamAggro
    {
        #region Attributes
        public Team team { get; set; }
        public List<float> charsAggro;
        #endregion

        #region Constructors
        public TeamAggro(Team team, float initCharsAggro) 
        {
            this.team = team;
            charsAggro = new List<float>(team.characters.Count);
            foreach (Character character in team.characters) 
            {
                charsAggro.Add(initCharsAggro);
            }
        }
        public TeamAggro(Team team) :
            this(team, CodeParameters.initCharAggro)
        {
        }
    #endregion

        #region Functionality
        public float getTeamAggro() 
        {
            float totalAggro = 0;
            for (int i = 0; i < charsAggro.Count; i++) 
            {
                Character character = team.characters[i];
                if (character.hp > 0) //not dead
                {
                    totalAggro += charsAggro[i];
                }
            }
            return totalAggro;
        }
        public Character getHighestCharacterAggro() 
        {
            Character highestAggroChar = null;
            float maxAggro = 0;
            for (int i = 0; i < charsAggro.Count; i++) 
            {
                Character character = team.characters[i];
                if (character.hp > 0 && (charsAggro[i] > maxAggro ||
                    charsAggro[i] == maxAggro && character.hp < team.characters[i - 1].hp)) 
                    //not dead & has the biggest aggro until now or if it's the same it has less hp
                {
                    maxAggro = charsAggro[i];
                    highestAggroChar = character;
                }
            }
            if (highestAggroChar == null)
                throw new Exception("highestaggrochar null");
            return highestAggroChar;
        }
        public static TeamAggro getHighestAggroTeam(List<TeamAggro> teamsAggresiveness ) 
        {
            TeamAggro mostAggresiveTeam = null;
            float highestAggro = 0;
            foreach (TeamAggro ta in teamsAggresiveness)
            { 
                float taAggro = ta.getTeamAggro();
                if (taAggro >= highestAggro)
                {
                    highestAggro = taAggro;
                    mostAggresiveTeam = ta;
                }
            }
            return mostAggresiveTeam;
        }
        #endregion
    }
}
